package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.GroupItem;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.Setmeal;
import com.xrit.sunnyday.service.SetMealService;
import com.xrit.sunnyday.utils.QiNiuUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @Reference
    private SetMealService setMealService;

    @GetMapping("/findAll")
    public Result findAll(){
        List<Setmeal> list = setMealService.findAll();
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,list);
    }


    @PostMapping("/findPage")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageInfo<Setmeal> pageInfo = setMealService.findPage(queryPageBean);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getList());
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
    }

    @PostMapping("/deleteById")
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    public Result deleteById(Integer id){
        setMealService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        //- 获取原有图片名称，截取到后缀名
        String originalFilename = imgFile.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        //- 生成唯一文件名，拼接后缀名
        String filename = UUID.randomUUID() + substring;
        try {
            QiNiuUtils.uploadViaByte(imgFile.getBytes(), filename);
            HashMap<String, String> map = new HashMap<>();
            map.put("imgName",filename);
            map.put("domain",QiNiuUtils.DOMAIN);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        setMealService.add(setmeal,checkgroupIds);
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @PostMapping("/findById")
    public Result findById(Integer id){
        HashMap<String, Object> map = null;
        map = setMealService.findById(id);
        List<Integer> list = setMealService.findSetmealGroupID(id);
        GroupItem groupItem = new GroupItem(map,list.toArray(new Integer[0]));
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,groupItem);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    public Result update(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        setMealService.update(setmeal,checkgroupIds);
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
}
